package com.zc.blog.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zc.blog.model.Article;
import com.zc.blog.model.Comment;
import com.zc.blog.model.Tag;
import com.zc.blog.service.IArticleService;
import com.zc.blog.service.ICommentService;
import com.zc.blog.service.ITagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;


@Controller
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private IArticleService articleService;

    @Autowired
    private ITagService tagService;

    @Autowired
    private ICommentService commentService;

    @Value("${pageSize}")
    private int pageSize;

    /**
     * 根据文章Id获取文章
     * @param articleId 文章id
     */
    @RequestMapping("/{articleId}")
    public String galleryPost(@PathVariable("articleId") Integer articleId, Model model) {
        Article article = articleService.findById(articleId); //获取文章信息
        article.setCountComment(commentService.count(new QueryWrapper<Comment>().eq("articleId", article.getId())));//设置评论数
        List<Tag> articleTagList = tagService.findTagsByArticleId(articleId); //获取文章标签
        List<Comment> commentList = commentService.findCommentByArticleId(articleId);//获取当前文章所有评论
        long countComment = commentService.count(new QueryWrapper<Comment>().eq("articleId", articleId));//该文章总评论数
        articleService.incrementClick(articleId); //添加点击量
        model.addAttribute("commentList", commentList);  //评论
        model.addAttribute("article", article);               //文章实体
        model.addAttribute("articleTagList", articleTagList); //标签列表
        model.addAttribute("countComment", countComment);  //文章总评论数
        return "blog/articleRead";
    }

}
